<?php
//连接mangodb
$manager = new MongoDB\Driver\Manager("mongodb://phpadmin:154263@localhost:27017/php");
// 插入
// $bulk=new MongoDB\Driver\BulkWrite;
// $bulk->insert(['name'=>'张飞','age'=>12,'email'=>'123@abc.com']);
// $bulk->insert(['name'=>'刘备','age'=>15,'email'=>'124@abc.com']);
// $bulk->insert(['name'=>'关羽','age'=>13,'email'=>'125@abc.com']);
// $manager->executeBulkWrite('php.class2',$bulk);
// echo 'OK';

//查询
//条件
// $filter=['age'=>['$lt'=>14]];
// $options=[
//      //不显示id
//      'projection'=>['_id'=>0],
//      //年龄降序
//      'sort'=>['name'=>1]
// ];
// //创建查询对象
// $query =new MongoDB\Driver\Query($filter,$options);
// //执行查询对象(表名,查询对象)
// $data=$manager->executeQuery('php.class2',$query);
// echo '<pre>';
// foreach($data as $k=>$v){
//      print_r($v);
// } 

//更新
// $bulk=new MongoDB\Driver\BulkWrite;
// // 更新条件
// $bulk->update(
//      // 条件
//      ['age'=>12],
//      //修改器
//      ['$set'=>['name'=>'刘备','email'=>'liubei@abc.com']],  
//      //附带条件 upsert存在更新否则新增 multi false只更新一条 true全部更新
//      ['multi'=>false,'upsert'=>true]
// );
// $writeConcern= new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,1000);
// $result=$manager->executeBulkWrite('php.class2',$bulk,$writeConcern);
// echo 'ok';

//删除
$bulk=new MongoDB\Driver\BulkWrite;
// [条件],[附带条件]
$bulk->delete(['age'=>12],['limit'=>1]);
$writeConcern= new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY,1000);
$result=$manager->executeBulkWrite('php.class2',$bulk,$writeConcern);
echo 'ok';
?>